package io.github.talelin.latticy.service.mq.impl;

import io.github.talelin.latticy.bo.StockBO;
import io.github.talelin.latticy.bo.StockOnTheWayBO;
import io.github.talelin.latticy.common.util.SnowFlake;
import io.github.talelin.latticy.feign.WmsMsgLogFeignClient;
import io.github.talelin.latticy.model.MsgLogDO;
import io.github.talelin.latticy.mq.MessageHelper;
import io.github.talelin.latticy.service.mq.StockProducerService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @authoer:wulongbo
 * @createDate:2022/8/1
 * @description:
 */
@Service
public class StockProducerServiceImpl implements StockProducerService {

    @Autowired
    private WmsMsgLogFeignClient wmsMsgLogFeignClient;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void deliveryOnTheWayStock(List<StockBO> stockBOS) {
        try {
            //生成消息id
            SnowFlake idWorker = new SnowFlake(0, 0);
            Long msgId = idWorker.nextId();
            StockOnTheWayBO stockOnTheWayBO = new StockOnTheWayBO(msgId, stockBOS);
            MsgLogDO msgLog = new MsgLogDO(msgId, stockOnTheWayBO, "inbound.order.no.exchange", "inbound.order.no.queue.key");
            wmsMsgLogFeignClient.createMsgLog(msgLog);// 消息入库
            CorrelationData correlationData = new CorrelationData(String.valueOf(msgId));
            rabbitTemplate.convertAndSend("inbound.order.no.exchange", "inbound.order.no.queue.key", MessageHelper.objToMsg(stockOnTheWayBO), correlationData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
